package fan.project1_SpeedTalkSystem.Controller;

import fan.project1_SpeedTalkSystem.Model.*;
import fan.project1_SpeedTalkSystem.Service.BlogService;
import fan.project1_SpeedTalkSystem.Service.UserService;
import io.jsonwebtoken.Claims;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    BlogService blogService;

    @RequestMapping("/register")
    public Result<String> register(String userName, String password, String githubUrl) {

        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return Result.fail("用户名或密码为空","");
        }

        UserInfo userInfo = userService.getUserInfoByName(userName);
        if(userInfo!=null) {
            return Result.fail("该用户已存在","");
        }

        UserInfo newUser = new UserInfo();
        newUser.setPassword(MD5Utils.getSecretPassword(password));
        newUser.setUserName(userName);
        newUser.setGithubUrl(githubUrl);


        try{
            if(userService.insertUser(newUser)==1){
                return  Result.success("OK");
            }else return Result.fail("注册失败","尝试插入数据库时失败");
        }catch (Exception e) {
            log.info("注册时插入数据库失败",e.getMessage());
            Result.fail("注册失败","尝试插入数据库时失败");
        }

        return null;

    }


    @RequestMapping("/login")
    public Result<String> login(String userName, String password) {

        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return Result.fail("用户名或密码为空","");
        }
        UserInfo userInfo = userService.getUserInfoByName(userName);
        if(userInfo==null) {
            return Result.fail("用户名不存在","");
        }
        if(!MD5Utils.checkPassword(password, userInfo.getPassword())) {
            return Result.fail("密码错误","");
        }

        HashMap<String, Object> msg = new HashMap<>();
        msg.put("userName", userName);
        msg.put("id", userInfo.getId());
        return Result.success(JWTUtils.getToken(msg));
    }

    @RequestMapping("/getUserInfoByBlogId")
    public Result getUserInfoByBlogId(int id) {
        if(id <=0) {return Result.fail("blogID为空","");}
        Integer userId = blogService.getUserIdByBlogId(id);
        ProUserInfo out =userService.getProUserInfoById(userId);
        return Result.success(out);
    }
    @RequestMapping("/getUserInfoByToken")
    public Result getUserInfoByToken(HttpServletRequest request) {
        log.info("list页面用户信息被调用");
        String userToken = request.getHeader("userToken");
        Claims msg = JWTUtils.getMsg(userToken);
//        if(msg==null){return Result.fail("用户未登录","");}
        Integer id = (Integer) msg.getOrDefault("id", "未获取到用户名");
        ProUserInfo proUserInfo = userService.getProUserInfoById(id);
        if(proUserInfo==null) {
            return Result.fail("未获取到用户信息，请检查输入","");
        }
        return Result.success(proUserInfo);
    }

}
